#include <Wire.h>  //调用arduino自带的I2C库
#include <stdio.h>
#include <SPI.h>
#include <WiFi.h>

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int weight=A0;//定义压力接口为模拟0 接口

int weight_data=0;//定义数字变量

int buttonState = 0; 
int temp=0;
char server[] = "192.168.31.167";
WiFiClient client;
//std::stringstream ss;
char ssid[] = "Xiaomi_C413"; //  your network SSID (name) 
char pass[] = "58602qwasz";    // your network password (use for WPA, or use as key for WEP)

char result[200];

int status = WL_IDLE_STATUS;
char deviceId[] = "C28C";
unsigned long lastConnectionTime = 0;           // last time you connected to the server, in milliseconds
boolean lastConnected = false;                  // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds


void setup()
{
	Serial.begin(9600);	//初始化串口
        while (!Serial) {
            ; // wait for serial port to connect. Needed for Leonardo only
        }
        
        if (WiFi.status() == WL_NO_SHIELD) {
          Serial.println("WiFi shield not present"); 
          // don't continue:
          while(true);
        } 
      
        String fv = WiFi.firmwareVersion();
        if( fv != "1.1.0" )
          Serial.println("Please upgrade the firmware");

        // attempt to connect to Wifi network:
          while (status != WL_CONNECTED) { 
            Serial.print("Attempting to connect to SSID: ");
            Serial.println(ssid);
            // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
            status = WiFi.begin(ssid, pass);
          
            // wait 10 seconds for connection:
            delay(10000);
          } 
          Serial.println("Connected to wifi");
        printWifiStatus();
        
        /*if (client.connect(server, 8001)) {
              Serial.println("connected to server");
        }*/
        
	delay(20);
	Wire.begin();			//初始化I2C
	delay(20);
        // initialize the LED pin as an output:
        pinMode(ledPin, OUTPUT);      
        // initialize the pushbutton pin as an input:
        pinMode(buttonPin, INPUT);    	

	delay(1000);
}

void loop()
{
	buttonState = digitalRead(buttonPin);
	
       if(buttonState==LOW) 
          {
            if(temp==0)
              temp=1;
            else if(temp==1)
              temp=0;
          }
          if(temp==1)
          {
            //next = 0;
                digitalWrite(ledPin, HIGH);
	      weight_data=analogRead(weight);//读取传感器的模拟值 

	      /*
                //输出模拟值，并将其打印出来
              Serial.print("weighte_data=");
              Serial.print(weight_data);
              Serial.print(" ");

         */
             
             sprintf(result, "GET /SmartHome/receiveSensorDataAction?deviceId=%s&weight=%d HTTP/1.1", deviceId, weight_data); 
             
                client.stop();
                httpRequest(result);
         
          }
          else
          digitalWrite(ledPin, LOW);
     delay(1000);
}

// this method makes a HTTP connection to the server:
void httpRequest(char result[]) {
  // if there's a successful connection:
  if (client.connect(server, 8001)) {
    client.println(result);
    client.println("Host: 192.168.31.167");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}


